<template>
	<view>
		<view v-bind:style="{width:videoObj.width+'upx',height:videoObj.height+'upx'}">
			<video style="width: 100%;" :src="videoObj.tempFilePath"></video>
		</view>
		<view class="cu-bar search bg-white">
			<view class="search-form round">
				<text class="cuIcon-search"></text>
				<input @focus="InputFocus" @blur="InputBlur"  type="text" placeholder="搜索更多bgm" confirm-type="search"></input>
			</view>
			<view class="action">
				<button class="cu-btn bg-green shadow-blur round">搜索</button>
			</view>
			<view class="action">
				<button class="cu-btn bg-green shadow-blur round">搜索</button>
			</view>
		</view>
		<scroll-view scroll-y="true" style="height: 700upx;">
			<view class="cu-list menu sm-border " style="margin-top: 10upx;" >
				<view class="cu-item" v-for="item in bgm" :key="item.id">
					<view class="content flex" :data-item="item" @click="playMusic">
						<text class="iconfont icon-yinyue text-grey" style="flex: 1;"></text>
						<text class="text-grey" style="flex: 6;">{{item.name}}</text>
						<text v-if="curBgm == item.id" class="text-blue iconfont icon-bofang "  style="font-size: 32px;"></text>
					</view>
				</view>
			</view>
		</scroll-view>
		
		<view style="margin:20upx 100upx 150upx 100upx ;">
			<button  @click="videoUpload" class="cu-btn block bg-green margin-tb-sm lg"><text class="iconfont icon-shangchuan-" style="margin-right: 20upx;font-size: 40upx;"></text>上传</button>
			<button  @click="back" class="cu-btn block bg-white margin-tb-sm lg"><text class="iconfont icon-shangchuan-" style="margin-right: 20upx;font-size: 40upx;"></text>返回</button>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	}from 'vuex'
	export default {
		computed:{
			...mapState(['userInfo','webUrl']),
		},
		onLoad(e) {
			this.videoObj = e
			const innerAudioContext = uni.createInnerAudioContext();
			this.audioContext = innerAudioContext
			uni.request({
				url: this.webUrl+"selectBgm",
				success: (res) => {
					this.bgm = res.data.data;
				}
			})
		},
		
		data() {
			return {
				bgm:'',
				curBgm:'',
				isPlay:false,
				audioContext:'',
				videoObj:{}
			};
		},
		methods:{
			playMusic: function(e) {
				var item = e.currentTarget.dataset.item
				this.curBgm = item.id
				if(this.audioContext.src == this.webUrl+item.path){
					this.audioContext.pause();
					this.curBgm = ''
				}else {
					this.audioContext.src = this.webUrl+item.path;
					this.audioContext.play()
					this.audioContext.onPlay(() => {
					  console.log('开始播放');
					});
					this.audioContext.onError((res) => {
					  console.log(res.errMsg);
					  console.log(res.errCode);
					});		
				}
						
			},
			back(){
				uni.navigateBack({
					
				})
			},
			videoUpload(){
				this.audioContext.pause();
				var userid = this.userInfo.id
				var bgmid = this.curBgm
				var file = this.videoObj.tempFilePath
				var seconds = this.videoObj.duration
				var height = this.videoObj.height
				var width = this.videoObj.width
				uni.showLoading({
					title:"上传中"
				})
				uni.uploadFile({
					url:this.webUrl+"videoUpload",
					filePath: file,
					name: 'file',
					formData: {
						'userId':userid,
						'bgmId' :bgmid,
						'seconds':seconds,
						'width':width,
						'height':height,
						'desc':''
					},
					success:(uploadFileRes) => {
						if(uploadFileRes.statusCode==200){
							console.log(uploadFileRes.data)
							console.log(uploadFileRes.statusCode)
							var obj = JSON.parse(uploadFileRes.data);
							console.log(obj)
							uni.showToast({
								title:"上传成功",
								duration:3000
							})
							
							var now = "login"
							
							this.$store.dispatch('NavChanges',now)
						}
					  	if(uploadFileRes.statusCode==404){
							console.log(uploadFileRes.data)
							console.log(uploadFileRes.statusCode)
							uni.hideLoading();
							that.modalName=null
							uni.showToast({
								title:"上传失败",
								icon:"none"
							})
						}
						return;
					}
				})
			}
		}
	}
</script>

<style lang="stylus">
	.bgm_item {
		width:100%;
		height:100upx;
	}
</style>
